<template>
  <a-layout>
    <a-layout-content
      :style="{
        background: '#fff',
        padding: '24px',
        margin: 0,
        minHeight: '280px',
      }"
    >
      <p>
        <a-form layout="inline" :model="param">
          <a-form-item>
            <a-input v-model:value="param.login_name" placeholder="登陆名">
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-button
              type="primary"
              @click="handleQuery({ page: 1, size: pagination.pageSize })"
            >
              查询
            </a-button>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="add()"> 新增 </a-button>
          </a-form-item>
        </a-form>
      </p>
      <a-table
        :columns="columns"
        :row-key="(record:any) => record.id"
        :data-source="users"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template v-slot:bodyCell="{ column, record }">
          <template v-if="column.title === 'Action'">
            <a-space size="small">
              <a-button type="primary" @click="handlerResetPassword(record)">
                重置密码
              </a-button>
              <a-button type="primary" @click="edit(record)"> 编辑 </a-button>
              <a-popconfirm
                title="删除后不可恢复，确认删除?"
                ok-text="是"
                cancel-text="否"
                @confirm="handleDelete(record.id)"
              >
                <a-button type="danger"> 删除 </a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>
  <a-modal title="用户表单" v-model:visible="modalVisible" @ok="handleModalOk">
    <a-form :model="user" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="登陆名">
        <a-input v-model:value="user.login_name" :disabled="!!user.id" />
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="user.name" />
      </a-form-item>
      <a-form-item label="密码" v-show="!user.id">
        <a-input v-model:value="user.password" type="password" />
      </a-form-item>
    </a-form>
  </a-modal>

  <!-- 充值密码 -->
  <a-modal
    title="重置密码"
    v-model:visible="resetModalVisible"
    @ok="handleResetModalOk"
  >
    <a-form :model="user" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="新密码">
        <a-input v-model:value="user.password" type="password" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  getUserListByPage,
  removeUser,
  editUser,
  resetPassword,
} from "@/api/user";
import { message } from "ant-design-vue";
import { hexMd5, KEY } from "@/util/md5";

const param = ref();
param.value = {};
const users = ref();
const pagination = ref({
  current: 1,
  pageSize: 4,
  total: 0,
});
const loading = ref(false);
const columns = [
  {
    title: "登陆名",
    dataIndex: "login_name",
  },
  {
    title: "名称",
    dataIndex: "name",
  },
  {
    title: "密码",
    dataIndex: "password",
  },
  {
    title: "Action",
    key: "action",
    dataIndex: "action",
  },
];

/*
 * 数据查询
 * */
const handleQuery = (params: any) => {
  // console.log(params);

  loading.value = true;
  //如果不清空现有数据，则编辑保存重新加载数据后，再点击编辑，则列表显示的还是编辑之前的数据
  users.value = [];
  getUserListByPage({
    page: params.page,
    size: params.size,
    login_name: param.value.login_name,
  }).then((res) => {
    loading.value = false;
    if (res.data.code === 10001) {
      users.value = res.data.data.list;

      //重置分页按钮
      pagination.value.current = params.page;
      pagination.value.total = res.data.data.total;
    } else {
      message.error(res.data.msg);
    }
  });
};
/*
 * 表格点击页码时触发
 * */
const handleTableChange = (pagination: any) => {
  // console.log("看看自带分页的参数都有些啥：" + pagination);
  handleQuery({
    page: pagination.current,
    size: pagination.pageSize,
  });
};

// -------- 表单 ---------
const user = ref();
const modalVisible = ref(false);
const modalLoading = ref(false);

// 显示新增
const add = () => {
  modalLoading.value = false;
  user.value = {};
  modalVisible.value = true;
};

// 添加
const handleModalOk = () => {
  modalLoading.value = true;
  // console.log("获取数据");
  editUser(user.value).then((res) => {
    // console.log(res);

    if (res.data.code === 10001) {
      modalVisible.value = false;

      //重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      message.error(res.data.msg);
    }
  });
};
// 删除
const handleDelete = (id: number) => {
  removeUser(id).then((res) => {
    // console.log(res);

    if (res.data.code === 10001) {
      //重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    }
  });
};
// 编辑
const edit = (record: any) => {
  modalLoading.value = false;
  //   -------------------------------
  user.value = JSON.parse(JSON.stringify(record));
  modalVisible.value = true;
};
const resetModalVisible = ref(false);
// 显示重置密码
const handlerResetPassword = (record: any) => {
  resetModalVisible.value = true;
  user.value = JSON.parse(JSON.stringify(record));
  user.value.password = null;
};
// 重置密码
const handleResetModalOk = () => {
  user.value.password = hexMd5(user.value.password + KEY);
  resetPassword(user.value).then((res) => {
    if (res.data.code === 10001) {
      resetModalVisible.value = false;
      // 重新加载列表
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    } else {
      message.error(res.data.msg);
    }
  });
};

onMounted(() => {
  handleQuery({
    page: 1,
    size: pagination.value.pageSize,
  });
});
</script>
